package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"goproject/commonType"
	"goproject/logger"
	"goproject/traceHandler"
	"net/http"
)

func GetTraceTreeData(c *gin.Context) {
	log := logger.GetLogger()
	log.Infof("请求到了")
	// 从请求体中获取 selectedReq
	var selectedReq commonType.SelectedReq
	if err := c.ShouldBindJSON(&selectedReq); err != nil {
		log.Errorf("绑定请求JSON时出错: %v", err)
		fmt.Printf("err: ", err)
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	log.Infof("收到的请求: %+v", selectedReq)

	// 调用 traceHandler/handler.go 中的 GetTraceData 函数获取 traceTree
	traceTree, err := traceHandler.GetTraceData(c.Request.Context(), selectedReq)
	if err != nil {
		log.Errorf("获取追踪数据时出错: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, traceTree)
}
